package io.dshubs.archives.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.dshubs.archives.dao.ArchiveBaseClassMapper;
import io.dshubs.archives.dao.ArchiveBaseStoreMapper;
import io.dshubs.archives.dao.ArchiveWarehouseLocationMapper;
import io.dshubs.archives.entity.*;
import io.dshubs.archives.service.ArchiveBaseStoreService;
import io.dshubs.commons.mybatis.enums.DelFlagEnum;
import io.dshubs.commons.mybatis.service.BaseService;
import io.dshubs.commons.mybatis.service.impl.BaseServiceImpl;
import io.dshubs.commons.security.user.SecurityUser;
import io.dshubs.commons.security.user.UserDetail;
import io.dshubs.commons.tools.constant.Constant;
import io.dshubs.commons.tools.enums.SuperAdminEnum;
import io.dshubs.commons.tools.enums.SuperTenantEnum;
import io.dshubs.commons.tools.page.PageData;
import io.dshubs.commons.tools.utils.Result;
import io.dshubs.dto.SysTenantDTO;
import io.dshubs.feign.UserFeignClient;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 库存表 服务实现类
 * </p>
 *
 * @author tan
 * @since 2021-08-26
 */
@Service
public class ArchiveBaseStoreServiceImpl extends BaseServiceImpl<ArchiveBaseStoreMapper, ArchiveBaseStore> implements ArchiveBaseStoreService {
    @Autowired
    private ArchiveWarehouseLocationMapper archiveWarehouseLocationMapper;

    @Autowired
    private ArchiveBaseClassMapper archiveBaseClassMapper;

    @Autowired
    private UserFeignClient userFeignClient;

    @Override
    public List<ArchiveBaseStore> getBySegmentId(Long id) {
        return this.baseDao.selectList(new QueryWrapper<ArchiveBaseStore>().eq("location_id",id)
        .eq("file_type",1)
        );
    }

    @Override
    public PageData<ArchiveBaseStore> page(Map<String, Object> params) {
        IPage<ArchiveBaseStore> page = baseDao.selectPage(
                getPage(params, Constant.CREATE_DATE, false),
                getWrapper(params)
        );
        PageData<ArchiveBaseStore> pageData = getPageData(page, ArchiveBaseStore.class);
        List<ArchiveBaseStore> list = pageData.getList();
        if(list.size() > 0) {
            list.forEach(item->{
                ArchiveWarehouseLocation location = archiveWarehouseLocationMapper.selectById(item.getLocationId());
                item.setSegmentNo(location == null ? null:location.getSegmentNo());
                ArchiveBaseCategory category = archiveBaseClassMapper.selectById(item.getCategoryId());
                item.setCategoryName(category == null ? null:category.getName());
            });
        }
        return pageData;
    }

    private QueryWrapper<ArchiveBaseStore> getWrapper(Map<String, Object> params){
        List<Long> ids = null;
        UserDetail user = SecurityUser.getUser();
        if(user.getSuperTenant() != SuperTenantEnum.YES.value()) {
            ids = userFeignClient.getCategoryId().getData();
        }
        String fileNumber = (String)params.get("fileNumber");
        QueryWrapper<ArchiveBaseStore> wrapper = new QueryWrapper<>();
        wrapper.eq(Constant.DEL_FLAG, DelFlagEnum.NORMAL.value());
        wrapper.like(StringUtils.isNotBlank(fileNumber), "file_number", fileNumber);
        if(CollectionUtils.isNotEmpty(ids)) {
            wrapper.in("category_id",ids);
        }
        if(user.getSuperAdmin() != SuperAdminEnum.YES.value()) {
            wrapper.eq("tenant_code",user.getTenantCode());
        }
        return wrapper;
    }
}
